﻿using PayexpDAL;
using SpreadsheetLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace ExcelHandler
{
    public class FilteredTransactionsExportToExcel : ExportToExcelBase
    {
        
        public FilteredTransactionsExportToExcel(string destinationFileFullPath, bool batchMode, FilterCriteria usedFilters) 
            : base(destinationFileFullPath, batchMode)
        {
            if (usedFilters == null)
            {
                throw new Exception("Please provide the filters used");
            }

            this._usedFilters = usedFilters;

            CreateSummary(); // I cannot put this in the base class :)
        }


        protected override void CreateSummary()
        {
            base.CreateSummary();

            this._currentDataRowIndex++; // VERY IMPORTANT! :)
            string rowIndexString = this._currentDataRowIndex.ToString();
            this._slDocument.SetCellValue("A" + rowIndexString, "Report Type");
            this._slDocument.SetCellStyle("A" + rowIndexString, "B" + rowIndexString, this._summaryKeyStyle);
            this._slDocument.MergeWorksheetCells("A" + rowIndexString, "B" + rowIndexString);
            this._slDocument.MergeWorksheetCells("C" + rowIndexString, "E" + rowIndexString);

            string cellValue = "Filtered Transaction Report.";
            if (!this._usedFilters.RunningBalanceComputed)
            {
                cellValue = cellValue + " Running Balance Not Computed."; 
            }
            if(this._usedFilters.TransactionDateSelected)
            {
                cellValue = cellValue + " Sorted By Transaction Date.";
            }
            else
            {
                cellValue = cellValue + " Sorted By Bank Date.";
            }

            this._slDocument.SetCellValue("C" + rowIndexString, cellValue);
            this._slDocument.SetCellStyle("C" + rowIndexString, "E" + rowIndexString, this._summaryValueStyle);
            //this._slDocument.AutoFitRow(this._currentDataRowIndex);

            // this is just a hack becuase merged cells and wrap do not work in excel...known issue :(
            this._slDocument.SetCellValue("CK" + rowIndexString, cellValue);
            this._slDocument.SetCellStyle("CK" + rowIndexString, this._summaryValueStyle);
            this._slDocument.SetColumnWidth("CK", 50);

            this._currentDataRowIndex++; // VERY IMPORTANT! :)
            rowIndexString = this._currentDataRowIndex.ToString();
            this._slDocument.SetCellValue("A" + rowIndexString, "Filters Used");
            this._slDocument.SetCellStyle("A" + rowIndexString, "B" + rowIndexString, this._summaryKeyStyle);
            this._slDocument.MergeWorksheetCells("A" + rowIndexString, "B" + rowIndexString);
            this._slDocument.MergeWorksheetCells("C" + rowIndexString, "E" + rowIndexString);
            this._slDocument.SetCellValue("C" + rowIndexString, this._usedFilters.FiltersUsedAsText);
            this._slDocument.SetCellStyle("C" + rowIndexString, "E" + rowIndexString, this._summaryValueStyle);
            //this._slDocument.AutoFitRow(this._currentDataRowIndex);

            // this is just a hack becuase merged cells and wrap do not work in excel...known issue :(
            this._slDocument.SetCellValue("CK" + rowIndexString, this._usedFilters.FiltersUsedAsText);
            this._slDocument.SetCellStyle("CK" + rowIndexString, this._summaryValueStyle);
            this._slDocument.SetColumnWidth("CK", 50);

            this._currentDataRowIndex++; // one empty row
            base.CreateDataRowsHeader();

        }
    }
}
